package com.zhanbo.zhanbowymusic.main.mine.like;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.mwx.baselib.base.BaseBindingAdapter;
import com.mwx.baselib.base.BaseFragment;
import com.mwx.baselib.bean.PageSongListBean;
import com.mwx.baselib.bean.SongBean;
import com.mwx.baselib.net.BaseResponses;
import com.mwx.baselib.net.DefaultObserver;
import com.mwx.baselib.net.NetworkManager;
import com.mwx.baselib.utils.GlideUtils;
import com.zhanbo.zhanbowymusic.R;
import com.zhanbo.zhanbowymusic.databinding.FragmentLikeBinding;
import com.zhanbo.zhanbowymusic.main.mine.like.adapter.LikeMusicListAdapter;
import com.zhanbo.zhanbowymusic.music.MusicController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 我喜欢fragment
 */
public class LikeFragment extends BaseFragment<FragmentLikeBinding> {


    @Override
    protected void init(Bundle savedInstanceState) {
        initView();
        initListener();
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
            super.onLazyInitView(savedInstanceState);
//            initData();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden){
            initData();
        }
        super.onHiddenChanged(hidden);
    }

    @Override
    protected Object getLayout() {
        return R.layout.fragment_like;
    }

    @Override
    protected void initData() {
        Map<String,String> map = new HashMap<>();
        NetworkManager.getInstance().getApiService().likeList(map)
                .subscribeOn(Schedulers.io())
                .compose(this.bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new DefaultObserver<BaseResponses<PageSongListBean>>(_mActivity) {
                    @Override
                    public void onSuccess(BaseResponses<PageSongListBean> listBaseResponses) {
                        binding.tvNum.setText(listBaseResponses.getData().getItems().size()+"");
                        if (listBaseResponses.getData().getItems()!=null&&listBaseResponses.getData().getItems().size()>0){
                            GlideUtils.loadImageRounded10(_mActivity,listBaseResponses.getData().getItems().get(0).getCoverImg(),binding.ivLike);

                        }
                        binding.recyclerLike.setLayoutManager(new LinearLayoutManager(_mActivity,LinearLayoutManager.VERTICAL,false));
                        LikeMusicListAdapter likeMusicListAdapter = new LikeMusicListAdapter(_mActivity);
                        likeMusicListAdapter.getItems().addAll(listBaseResponses.getData().getItems());
                        binding.recyclerLike.setAdapter(likeMusicListAdapter);
                        likeMusicListAdapter.setOnItemClickListener(new BaseBindingAdapter.OnItemClickListener<SongBean>() {
                            @Override
                            public void onItemClick(int position) {
                                MusicController.getInstance().updateItemDate(likeMusicListAdapter.getItems().get(position));
                            }
                        });
                        binding.ivPlayAll.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                MusicController.getInstance().updateAndPlaySheetData(likeMusicListAdapter.getItems());
                            }
                        });

                    }
                });
    }

    @Override
    protected void initView() {


    }

    @Override
    protected void initListener() {

    }
    public static LikeFragment newInstance() {

        Bundle args = new Bundle();

        LikeFragment fragment = new LikeFragment();
        fragment.setArguments(args);
        return fragment;
    }
}